package cn.lopr.im.api.controller;

import cn.lopr.im.api.annotation.Auth;
import cn.lopr.im.api.bean.*;
import cn.lopr.im.api.common.CallbackMessage;
import cn.lopr.im.api.configuration.AuthInfo;
import cn.lopr.im.api.service.IArticleService;
import cn.lopr.im.api.vo.ArticleCommentVo;
import cn.lopr.im.api.vo.ArticleVo;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author shuangyang7
 * @date 2021/5/21 18:17
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    @Auth
    @RequestMapping("/getPlate")
    @ResponseBody
    public CallbackMessage<?> getPlate() {
        List<ArticlePlate> articlePlates = articleService.getPlate();
        return CallbackMessage.createBySuccess(articlePlates);
    }

    @Auth
    @RequestMapping("/getArticleList")
    @ResponseBody
    public CallbackMessage<?> getArticleList(@RequestParam(defaultValue = "1") int page,
                                             @RequestParam(defaultValue = "20") int size, String plateId) {
        User user = AuthInfo.get();
        PageInfo<ArticleVo> articleVos = articleService.getArticleList(user, page, size, plateId);
        return CallbackMessage.createBySuccess(articleVos);
    }

    @Auth
    @RequestMapping("/getArticleListByUserId")
    @ResponseBody
    public CallbackMessage<?> getArticleListByUserId(@RequestParam(defaultValue = "1") int page,
                                                     @RequestParam(defaultValue = "20") int size, String userId) {
        User user = AuthInfo.get();
        PageInfo<ArticleVo> articleVos = articleService.getArticleListByUserId(user, page, size, userId);
        return CallbackMessage.createBySuccess(articleVos);
    }

    @Auth
    @RequestMapping("/getArticle")
    @ResponseBody
    public CallbackMessage<?> getArticle(String articleId) {
        User user = AuthInfo.get();
        ArticleVo articleVo = articleService.getArticle(user, articleId);
        return CallbackMessage.createBySuccess(articleVo);
    }

    @Auth
    @RequestMapping("/addArticle")
    @ResponseBody
    public CallbackMessage<?> addArticle(@RequestBody Article article) {
        User user = AuthInfo.get();
        boolean isOk = articleService.addArticle(user, article);

        if (isOk) {
            return CallbackMessage.createBySuccess("发布成功");
        }
        return CallbackMessage.createByError("发布失败");
    }

    @Auth
    @RequestMapping("/updateArticle")
    @ResponseBody
    public CallbackMessage<?> updateArticle(@RequestBody Article article) {
        User user = AuthInfo.get();
        boolean isOk = articleService.updateArticle(user, article);

        if (!isOk) {
            return CallbackMessage.createByError("更新失败");
        }

        String retMsg = "";
        if (article.getLike() != null && article.getLike() != 0) {
            retMsg = "点赞成功";
        }
        if (!StringUtils.isEmpty(article.getContent())) {
            retMsg = "更新成功";
        }

        return CallbackMessage.createBySuccess(retMsg);
    }

    @Auth
    @RequestMapping("/delArticle")
    @ResponseBody
    public CallbackMessage<?> delArticle(String articleId) {
        User user = AuthInfo.get();
        boolean articleVos = articleService.delArticle(user, articleId);

        if (articleVos) {
            return CallbackMessage.createByError("删除失败");
        }
        return CallbackMessage.createBySuccess("删除成功");
    }

    @Auth
    @RequestMapping("/getArticleComment")
    @ResponseBody
    public CallbackMessage<?> getArticleComment(@RequestParam(defaultValue = "1") int page,
                                                @RequestParam(defaultValue = "20") int size, String articleId) {
        List<ArticleCommentVo> articleCommentVos = articleService.getArticleComment(articleId, page, size);
        return CallbackMessage.createBySuccess(articleCommentVos);
    }

    /**
     * 获取评论树， 多级递归
     */
    //@Auth
    @RequestMapping("/getArticleChildCommentMultistage")
    @ResponseBody
    public CallbackMessage<?> getArticleChildCommentMultistage(@RequestParam(defaultValue = "1") int page,
                                                               @RequestParam(defaultValue = "20") int size,
                                                               String parentId) {
        List<ArticleCommentVo> articleCommentVos = articleService.getArticleChildCommentMultistage(parentId, page, size);
        return CallbackMessage.createBySuccess(articleCommentVos);
    }

    /**
     * 把多级评论拆分成，一级回复评论， Android端
     */
    @RequestMapping("/getArticleChildCommentSingle")
    @ResponseBody
    public CallbackMessage<?> getArticleChildCommentSingle(@RequestParam(defaultValue = "1") int page,
                                                           @RequestParam(defaultValue = "20") int size,
                                                           String test,
                                                           String parentId) {
        List<ArticleCommentVo> articleCommentVos = articleService.getArticleChildCommentSingle(parentId, page, size);
        return CallbackMessage.createBySuccess(articleCommentVos);
    }

    @Auth
    @RequestMapping("/addArticleComment")
    @ResponseBody
    public CallbackMessage<?> addArticleComment(@RequestBody ArticleComment articleComment) {
        User user = AuthInfo.get();
        boolean isOk = articleService.addArticleComment(user, articleComment);
        if (!isOk) {
            return CallbackMessage.createByError("评论发表失败，请重试！");
        }
        return CallbackMessage.createBySuccess();
    }

    @Auth
    @RequestMapping("/updateArticleComment")
    @ResponseBody
    public CallbackMessage<?> updateArticleComment(String articleId) {
        return CallbackMessage.createBySuccess();
    }

    @Auth
    @RequestMapping("/delArticleComment")
    @ResponseBody
    public CallbackMessage<?> delArticleComment(String articleId) {
        return CallbackMessage.createBySuccess();
    }

    @RequestMapping("/getArticleLike")
    @ResponseBody
    public CallbackMessage<?> getArticleLike(String articleId) {
        List<ArticleOption> articles = articleService.getArticleLike(articleId);
        return CallbackMessage.createBySuccess(articles);
    }

    @RequestMapping("/getArticleShare")
    @ResponseBody
    public CallbackMessage<?> getArticleShare(String articleId) {
        List<ArticleOption> articles = articleService.getArticleShare(articleId);
        return CallbackMessage.createBySuccess(articles);
    }

}
